<?php
/**
 * 获取热点词
 * User: liuser
 * Date: 2018/4/8
 * Time: 下午3:31
 */


include "vendor/autoload.php";
include "util.php";
include "DBPDO.php";
use Symfony\Component\DomCrawler\Crawler;
class information{

    public $msg;
    public $cookie = null;
    public $exchange = [
        'huobi'=>'火币Pro',
        'binance'=>'币安',
        'okex'=>'OKEX',
        'otcbtc'=>'OTCBTC',
        'hadax'=>'HADAX',
        'gate'=>'Gate.io',
        'coinyee'=>'币易',
    ];


    public function coinyee()
    {
        $url = 'http://www.coinyee.io/article';
        $crawler =  $this->parse($url);
        $key = 'coinyee';
        $exchange_title = $this->exchange[$key];
        $xpath = '//*[@class="article_list"][1]/li';
        $crawler->filterXPath($xpath)->each(function (Crawler $node,$i) use ($exchange_title) {
            $title = trim($node->filter('a')->text());
            $url   = trim($node->filter('a')->attr('href'));
            $create = trim($node->filter('span')->text());
            $c1 = $this->parse($url);
            $content = trim($c1->filter('.faq_content')->text());
            $this->save($title,$content,$exchange_title,$create,$url);
        });

    }
    public function run()
    {

        $keys = array_keys($this->exchange);
        $keys_count = count($keys);
//        $this->hadax();
        for ($i=0;$i<$keys_count;$i++) {
            $pid = pcntl_fork(); // 创建子进程
            if ($pid == 0 ) {
                $key = $keys[$i];
                $this->$key();
                exit($i);
            }
        }
        #挂起当前进程的执行直到参数pid指定的进程号的进程退出， 或接收到一个信号要求中断当前进程或调用一个信号处理函数。
        while (pcntl_waitpid(0, $status) != -1) {
            #pcntl_wexitstatus — 返回一个中断的子进程的返回代码
            $status = pcntl_wexitstatus($status);
            echo "Child $status completed\n";
        }



//        foreach($keys as $key) {
//            $this->$key();
//        }
//        #地址集合
//        $urls = $this->urls;
//        #地址总数
//        $urls_count = count($urls);
//        #进程数
//        $threadNum = 4;
//        #除完后取整
//        $split_len = ceil($urls_count / $threadNum);
//        $list = array_chunk($urls,$split_len);
//
//        #取整后的列表总数，也就是需要开多少个真实进程
//        $c_list = count($list);
//        #普通执行2分钟16秒
//        for ($i=0;$i<$c_list;$i++) {
//            $process = new swoole_process(function () use($list,$i){
//                $arr  = $list[$i];
//                foreach($arr as $key){
//                    $this->$key();
//                }
//            },false);
//            $pid = $process->start();
//        }

////        #火币
//        $this->huobi();
////        #币安
//        $this->binance();
////        #okex
//        $this->okex();
////        #otcbtc
//        $this->otcbtc();
////        #bitfinex
//        $this->bitfinex();
//        $this->hadax();
//
//
//



    }

    public function gate() {
        $url = 'https://gate.io/articlelist/ann';
        $base_url = 'https://gate.io/';
        $this->cookie = 'lang=cn';
        $crawler =  $this->parse($url);
        $key = 'gate';

        $exchange_title = $this->exchange[$key];
        if(empty($crawler)) {
            echo '未获取到信息 . from  ' . $key;
            return false;
        }
        $xpath = '//*[@id="lcontentnews"]/div';
        try{
            $crawler->filterXPath($xpath)->each(function (Crawler $node,$i) use($base_url,$exchange_title) {
                $title = trim($node->filter('h3')->text());
                $create = trim($node->filter('#updated')->text());
                $content = trim($node->filter('.news-brief')->text());
                $url = $base_url . $node->filter('a')->attr('href');
                $this->save($title,$content,$exchange_title,$create,$url);
            });
        }catch (\Exception $e) {
            echo 'gate.xpath提取失败';
        }


    }

    public function hadax() {
        $url = 'https://content.hadax.com/p/api/contents/hadax/list_notice?r=0p7dsvygjrfa&limit=10&language=zh-cn';
        $content_base_url = 'https://www.hadax.com/zh-cn/notice_detail/?id=';
        $html = util::curl($url);
        $arr = json_decode($html,1);
        if($arr['success'] == true) {
            foreach($arr['data']['items'] as $item) {
                $title = $item['title'];
                $create = rtrim($item['created'],'000');
                $content = $item['content'];
                $exchange_title = $this->exchange['hadax'];
                $url =$content_base_url . $item['id'];
                $this->save($title,$content,$exchange_title,date('Y-m-d H:i:s',$create),$url);
            }
        }
    }

    /**
     * 获取bitfinex资讯
     */
    public function bitfinex() {
        $url = 'https://www.bitfinex.com/posts';
        $base_url = 'https://www.bitfinex.com/';
        $crawler =  $this->parse($url);
        $key = 'bitfinex';
        $exchange_title = $this->exchange[$key];
        if(empty($crawler)) {
            echo '未获取到信息 . from  ' . $key;
            return false;
        }
        $xpath = '//*[@id="posts-page"]/div/div/div';
        $crawler->filterXPath($xpath)->each(function (Crawler $node,$i) use ($base_url,$exchange_title,$key) {
            try{
                $ori_title  = $node->filter('h5')->filter('a')->text();
                $title = trim($ori_title);
                $ori_content  = $node->filter('div')->text();
                $content = ltrim($ori_content);
                $url   =  $node->filter('a')->attr('href');
                echo $url;
                if(!$url) {
                    echo  $url;
                    return false;
                }
                $url = $base_url . $url;
                $c1 = $this->parse($url);
                if(!$c1 || empty($c1)){
                    echo 1;
                    return false;
                }
                $datetime= $c1->filterXPath('//*[@id="posts-page"]/div/div/div/h5/span[2]')->text();
//                $content = $c1->filterXPath('//*[@class="article-body"]')->text();
                $this->save($title,$content,$exchange_title,$datetime,$url);
            }catch (InvalidArgumentException $e){
                echo '发生错误'. $e->getMessage();
            }
        });
    }

    /**
     * 解析成crawler对象
     * @param $url 需要解析的url
     * @return bool|Crawler 返回crawler对象
     */
    public function parse($url)
    {
        $html = util::curl($url,$this->cookie);
        if(empty($html) || !$html){
          return false;
        }
        return new Crawler($html);
    }

    /**
     * 抓取otcbtc资讯
     * @return bool
     */
    public function otcbtc() {
        $url = 'https://support.otcbtc.com/hc/zh-cn/categories/360000039452-%E5%85%AC%E5%91%8A%E4%B8%AD%E5%BF%83';
        $base_url = 'https://support.otcbtc.com/';
        $xpath = '//*[@class="article-list"]/li';

        $crawler =  $this->parse($url);
        $key = 'otcbtc';
        $exchange_title = $this->exchange[$key];
        if(empty($crawler)) {
            echo '未获取到信息 . from  ' . $key;
            return false;
        }

        $crawler->filterXPath($xpath)->each(function (Crawler $node,$i) use ($base_url,$exchange_title,$key) {
            try{
                $title = trim($node->filter('a')->text());
                $url   =  $node->filter('a')->attr('href');
                if(!$url) {
                    return false;
                }
                $url = $base_url . $url;
                $c1 = $this->parse($url);
                if(!$c1 || empty($c1)){
                    return false;
                }
                $datetime= $c1->filterXPath('//*[@id="article-container"]/article/header/div/div[2]/ul/li[1]/time')->text();
                $content = $c1->filterXPath('//*[@class="article-body"]')->text();
                $this->save($title,$content,$exchange_title,$datetime,$url);
            }catch (InvalidArgumentException $e){
                echo '发生错误'. "来源:{$key}" . 'url ' . $url;
            }
        });
    }

    /**
     * 获取okex资讯
     */
    public function okex() {
        $url = 'https://support.okex.com/hc/zh-cn/sections/115000447632-%E5%85%AC%E5%91%8A%E4%B8%AD%E5%BF%83';
        $xpath = '//*[@class="article-list"]/li';
        $base_url = 'https://support.okex.com/';
        $crawler =  $this->parse($url);
        $exchange_title = 'OKEX';
        if(empty($crawler)) {
            echo '未获取到信息 . from  okex';
            return false;
        }

            $crawler->filterXPath($xpath)->each(function (Crawler $node,$i) use ($base_url,$exchange_title) {
                try{
                    $title = trim($node->filter('a')->text());
                    $url   =  $node->filter('a')->attr('href');
                    if(!$url) {
                        return false;
                    }
                    $url = $base_url . $url;
                    $c1 = $this->parse($url);
                    if(!$c1 || empty($c1)){
                        return false;
                    }
                    $datetime= $c1->filterXPath('//*[@id="article-container"]/article/header/div/div[2]/ul/li[1]/time')->text();
                    $content = $c1->filterXPath('//*[@class="article-body"]')->text();
                    $this->save($title,$content,$exchange_title,$datetime,$url);
                }catch (InvalidArgumentException $e){
                    echo '发生错误'. "来源:okex" . 'url ' . $url;
                }
            });


    }

    /**
     * 获取火币资讯
     */
    public function huobi()
    {

        $url = 'https://www.huobipro.com/-/x/hb/p/api/contents/pro/list_notice?r=zrwnoxmvmpd&limit=10&language=zh-cn';
        $content_base_url = 'https://www.huobipro.com/zh-cn/notice_detail/?id=';
//        $url  = "https://www.baidu.com";
        $html = util::curl($url);
        $arr = (json_decode($html,1));
        if($arr['success'] == 1) {
            foreach($arr['data']['items'] as $item) {
                $title = $item['title'];
                $create = rtrim($item['created'],'000');
                $content = $item['content'];
                $exchange_title = '火币Pro';
                $url =$content_base_url . $item['id'];
                $this->save($title,$content,$exchange_title,date('Y-m-d H:i:s',$create),$url);
            }
        }
    }

    /**
     * 获取币安资讯
     */
    public function binance()
    {
        $url = 'https://support.binance.com/hc/zh-cn/categories/115000056351-%E5%85%AC%E5%91%8A%E4%B8%AD%E5%BF%83';
        $html = util::curl($url);
        try{
            $base_url = 'https://support.binance.com/';
            #交易所名称
            $exchange_title = '币安';

            if(empty($html) || !$html){
                throw new \Exception( '未匹配到html');
            }
            $crawler = new Crawler($html);
            $xpath1 ='//*[@class="category-container"]/div/div/section/ul/li';
            $crawler->filterXPath($xpath1)->each(function (Crawler $node,$i) use ($base_url,$exchange_title){
                $title = $node->filter('a')->text();
                $url   = $node->filter('a')->attr('href');
                $url   = $base_url . $url;
                //抓取详情，获取时间和详情
                $html =  util::curl($url);
                $c1 =  new Crawler($html);
                $date = $c1->filterXPath('//*[@id="article-container"]/article/header/div/div[2]/ul/li/time')->text();
                $content = $c1->filterXPath('//*[@id="article-container"]/article/section[1]/div/div[1]')->text();

                $this->save($title,$content,$exchange_title,$date,$url);
            });

        }catch (\Exception $e){
            $this->msg = $e->getMessage();
            echo $this->msg;
        }
    }

    /**
     * 保存到数据库
     */
    public function save($title,$content,$exchange_title,$create,$url)
    {
       $dbh = new DBPDO();

        $search = "select id from coin_Information where `title` = '{$title}'";
        $search_res  = $dbh->fetch($search);
        if(!$search_res) {
            $content = substr(trim($content),0,288) . '......';
            $sql = "INSERT INTO `coin_Information` (`exchange_id`, `exchange_title`, `title`, `url`, `create_at`,`content`) 
                VALUES ('0', '{$exchange_title}', '{$title}', '{$url}','{$create}', '{$content}');";
            $res = $dbh->execute($sql);
        }


    }

}

$app = new information();
$app->run();





